/************获取数据**********/
    var getUserInfo={
        url:'user/detailsUser',
        fromwho:'ch',
        sendData:{
            id:uid
        }
    }
    getAjax(getUserInfo,
        function(data){
            if(data.result==1){
                $.each(data.data,function(k,v){
                    if(k=='headimage'){
                        $('#headimg')[0].src=catchurl(v)
                    }
                    $('input[name="'+k+'"]').val(v)
                })
            }else{
                mui.toast(data.msg)
            }
        },
        function(data){
            mui.toast('服务器出错')
        }
    )
/************获取数据end**********/

/************提交数据**********/
mui('.info-detail').on('tap','.saveInfo',function(){
    var realname=$('#realname').val()
    var mtel=$('#mtel').val()
	if(!realname){
		outLine('请输入用户名')
		return false
	}
	var myreg=/^[1][3,4,5,7,8][0-9]{9}$/
	if(!mtel||!myreg.test(mtel)){
		outLine('手机号码有误')
		return false
	}
	var editUserData={
		url:"user/updateUserByUid",
		fromwho:'ch',
		sendData:{
			realname:realname,
			mtel:mtel
		}
	}
	upAjax(editUserData,
		function(data){
			if(data.result==1){
			    var changeuserinfo=JSON.parse(localStorage.getItem('userInfo'))
			    changeuserinfo.realname=realname
			    changeuserinfo.mtel=mtel
			    var changeuserinfoString=JSON.stringify(changeuserinfo)
			    localStorage.setItem('userInfo',changeuserinfoString)
				mui.toast('修改成功,1s后自动跳转')
				setTimeout(function(){
                    mui.plusReady(function(){
                        var main = plus.webview.currentWebview().opener()
                        mui.fire(main, "pageflowrefresh" )
                        mui.openWindow({url:'index.html'});
                    });
				}, 1000)
			}else{
			    mui.toast(data.msg)
			}
		},
		function(data){
			mui.toast('服务器出错')
		}
	)
})
/************提交数据end**********/
/************上传头像**********/
mui.plusReady(function() {
    mui(".mui-content").on('tap','.addimg',function(){
        showActionSheet(this)
    })
/***********上传图片***********/
//选取图片的来源，拍照和相册  
    function showActionSheet(obj){  
          var divid = obj.id;  
          var actionbuttons=[{title:"拍照"},{title:"相册选取"}];  
          var actionstyle={title:"选择照片",cancel:"取消",buttons:actionbuttons};  
          plus.nativeUI.actionSheet(actionstyle, function(e){  
                if(e.index==1){  
                    getImage(divid);  
                }else if(e.index==2){  
                    galleryImg(divid);  
                }  
          });  
    } 
//调用相册
    function galleryImg(divid){
        plus.gallery.pick(function(path){
            upload(path)
        });
    }
    function getImage(divid) {  
        plus.camera.getCamera().captureImage(function(e){
            console.log("e is" +  e);
            plus.io.resolveLocalFileSystemURL(e,
                function(entry) {
                    var path = entry.toLocalURL();
                    upload(path)
                }, 
                function(e) {
                    mui.toast("读取拍照文件错误：" + e.message);
                }
            );
         
        });  
    }
    //服务端接口路径
    var server = httpurl('user/updateHeadImageByUid','ch');
    // 上传文件
    function upload(path){
        var wt=plus.nativeUI.showWaiting();
        var task=plus.uploader.createUpload(server,
            {method:"POST"},
            function(t,status){ //上传完成
                if(status==200){
                    console.log(JSON.parse(t.responseText))
                    if(JSON.parse(t.responseText).result==1){
                         mui.toast("上传成功");
                    }
                    document.getElementById("headimg").src = path;
                    wt.close(); //关闭等待提示按钮
                }else{
                     mui.toast("上传失败："+status);
                    wt.close();//关闭等待提示按钮
                }
            }
        );  
        //添加其他参数
    //  task.addData("name","test");
        task.addFile(path,{key:"headimage"});
        console.log(path)
        console.log(server)
        task.start();
    } 
})

/************上传头像end**********/